package com.example.thymeleaf.word.controller;

import com.example.thymeleaf.word.service.AsposeWordService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/demo")
public class DemoController {
    @Resource
    private AsposeWordService asposeWordService;

    /**
     * word下载
     *
     * @param response HttpServletResponse
     */
    @GetMapping(value = "/word/download")
    public void downloadWord(HttpServletResponse response) throws Exception {
        Map<String, Object> variables = new HashMap<>(4);
        variables.put("title", "测试预览Word!");
        variables.put("imageUrl", "http://localhost:8080/images/wxcode.jpg");
        List<Map<String, String>> demoList = new ArrayList<>();
        Map<String, String> demoMap = new HashMap<>(8);
        demoMap.put("name", "哈哈");
        demoMap.put("nick", "娃娃");
        demoMap.put("age", "19");
        Map<String, String> demoMap2 = new HashMap<>(8);
        demoMap2.put("name", "天天");
        demoMap2.put("nick", "饭饭");
        demoMap2.put("age", "14");
        demoList.add(demoMap);
        demoList.add(demoMap2);
        variables.put("demoList", demoList);
        asposeWordService.download("word", variables, response, "测试打印.docx");
    }

    /**
     * word预览
     *
     * @param response HttpServletResponse
     */
    @GetMapping(value = "/word/preview")
    public void previewWord(HttpServletResponse response) throws Exception {
        // 构造freemarker模板引擎参数,listVars.size()个数对应pdf页数
        Map<String, Object> variables = new HashMap<>(4);
        variables.put("title", "测试预览Word!");
        variables.put("imageUrl", "http://localhost:10001/images/wxcode.jpg");
        List<Map<String, String>> demoList = new ArrayList<>();
        Map<String, String> demoMap = new HashMap<>(8);
        demoMap.put("name", "哈哈");
        demoMap.put("nick", "娃娃");
        demoMap.put("age", "19");
        Map<String, String> demoMap2 = new HashMap<>(8);
        demoMap2.put("name", "天天");
        demoMap2.put("nick", "饭饭");
        demoMap2.put("age", "14");
        demoList.add(demoMap);
        demoList.add(demoMap2);
        variables.put("demoList", demoList);
        asposeWordService.preview("word", variables, response);
    }
}
